package com.framework.modules.base.controller;

import java.util.HashMap;
import java.util.Map;

import com.framework.common.config.SystemConfigName;
import com.framework.common.model.config.ConfigServers;
import com.framework.common.utils.QuickHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.framework.modules.base.service.NewsService;
import com.framework.common.utils.PageUtils;
import com.framework.common.utils.R;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


/**
 * 文章表 
 *
 * @author MIT
 * @email litmiss@gmail.com
 * @date 2020-10-27 10:46:10
 */
@RestController
@RequestMapping("/news")
@Api(tags = "资讯")
public class NewsController {
    @Autowired
    private NewsService newsService;


    /**
     * 前台资讯
     * @return
     */
    @CrossOrigin
    @GetMapping("item")
    @ApiOperation("前台资讯")
    public R item(Integer id) {
        Object object = newsService.findById(id);
        return R.ok(object);
    }

    /**
     * 获取资讯列表
     * 若是从 app　端获取该信息，并且使用 webview 访问内容，请格式化 app_link_url_template 字段模板 {id} = item.id
     * @param category
     * @param page
     * @return
     */
    @CrossOrigin
    @RequestMapping("list")
    @ApiOperation("资讯列表")
    public R item(String category,@RequestParam(value = "page",defaultValue = "1") String page) {
        Map<String, Object> para = new HashMap<>();
        para.put("page", page);
        para.put("category_id", category);
        para.put("limit", "15");
        PageUtils pagedata = newsService.queryPage(para);
        ConfigServers servers = QuickHelper.getConfigValue(SystemConfigName.SERVERS, ConfigServers.class);
        String tplurl = String.format("%s/html/#/news/item?id={id}", servers.getFrontServer());
        return R.ok(pagedata).put("category", category).put("app_link_url_template",tplurl);
    }
}
